/**
 * 
 */
package org.lora.webframework.scheduler.impl;

import java.util.List;
import java.util.Map;

import org.lora.biz.ou.LoraOUConstant;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.annotation.Service;
import org.lora.core.mybatis.OrderByBean;
import org.lora.core.mybatis.Page;
import org.lora.exception.LoraCoreException;
import org.lora.util.StringUtil;
import org.lora.webframework.common.FrameWorkBaseService;
import org.lora.webframework.scheduler.ISchedulerService;
import org.lora.webframework.scheduler.entity.QrtzJobDetail;
import org.lora.webframework.scheduler.entity.QrtzJobDetailExample;
import org.lora.webframework.scheduler.mapper.QrtzJobDetailMapper;

/**
 * <p>
 * 类名:SchedulerServiceImpl
 * </p>
 * 描述:<br>
 * 创建时间:2016年11月9日 下午4:10:51<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年11月9日 Bladnir 创建<br>
 */
@Service
public class SchedulerServiceImpl extends FrameWorkBaseService implements ISchedulerService {

	@AutoAssemble
	private QrtzJobDetailMapper qrtzJobDetailMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.webframework.scheduler.ISchedulerService#getAllJob(java.util
	 * .Map, org.lora.core.mybatis.Page, java.util.List)
	 */
	@Override
	public List<QrtzJobDetail> getAllJob(Map<String, Object> parameterMap, Page page, List<OrderByBean> orderByList)
			throws LoraCoreException {

		String orderbyStr = super.getOrderByClause(orderByList);

		QrtzJobDetailExample example = new QrtzJobDetailExample();

		org.lora.webframework.scheduler.entity.QrtzJobDetailExample.Criteria criteria = example.createCriteria();

		setParameterForAllJob(parameterMap, criteria);

		if (StringUtil.isNotNull(orderbyStr)) {
			example.setOrderByClause(orderbyStr);
		}

		return qrtzJobDetailMapper.selectByExample(example, page);
	}

	private void setParameterForAllJob(Map<String, Object> parameterMap,
			org.lora.webframework.scheduler.entity.QrtzJobDetailExample.Criteria criteria) {
		String jobName = getStringFromMap("jobName", parameterMap);
		if (StringUtil.isNotNull(jobName)) {
			criteria.andJobNameLike(LoraOUConstant.PERCENT + jobName + LoraOUConstant.PERCENT);
		}

		String jobGroup = getStringFromMap("jobGroup", parameterMap);
		if (StringUtil.isNotNull(jobGroup)) {
			criteria.andJobGroupLike(LoraOUConstant.PERCENT + jobGroup + LoraOUConstant.PERCENT);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.lora.webframework.scheduler.ISchedulerService#getJobCount(java.util
	 * .Map)
	 */
	@Override
	public int getJobCount(Map<String, Object> parameterMap) throws LoraCoreException {
		QrtzJobDetailExample example = new QrtzJobDetailExample();
		org.lora.webframework.scheduler.entity.QrtzJobDetailExample.Criteria criteria = example.createCriteria();

		setParameterForAllJob(parameterMap, criteria);

		return qrtzJobDetailMapper.countByExample(example);
	}

}
